IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'RealmUpdate')
	BEGIN
		DROP  Procedure  RealmUpdate
	END

GO

CREATE Procedure RealmUpdate
@RealmID int,
@Title varchar(300),
@Description ntext,
@Summary ntext,
@ParentID int,
@IsPrivate bit
AS
Update Realm 
Set 
Title = @Title,
Description = @Description,
Summary = @Summary,
ParentID = @ParentID,
IsPrivate = @IsPrivate,
ModifiedDate = GetDate()
Where RealmID = @RealmID

-- Root Realm Is set to Private need to set all sub realms to private
If( @IsPrivate = 1)
Begin 
;With Nodes AS 
(
	Select RealmID, Title, ParentID
	From Realm 
	Where RealmID = @RealmID
	Union All
	Select r.RealmID, r.Title, r.ParentID
	From Nodes as N
	Join Realm AS r
	On r.ParentID = N.RealmID
	
) 
Update Realm 
Set IsPrivate = 1
Where RealmID In (
Select RealmID FRom Nodes);
End
GO



